{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "collapsed_sections": [
        "iDI2dKfRWTId"
      ]
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Demo for stable-diffusion-pytorch"
      ],
      "metadata": {
        "id": "UhG5CzHQWNzr"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Install (takes about 1~5 minutes)"
      ],
      "metadata": {
        "id": "iDI2dKfRWTId"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "%cd /content\n",
        "!git clone https://github.com/kjsman/stable-diffusion-pytorch"
      ],
      "metadata": {
        "id": "AgkJdPCbVjf6"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Note that all depencdencies of stable-diffusion-pytorch is pre-installed\n",
        "# on Colab environment. This cell basically does nothing on Colab.\n",
        "%cd /content/stable-diffusion-pytorch\n",
        "%pip install -r requirements.txt"
      ],
      "metadata": {
        "id": "uUsTYf-6BZGs"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "%cd /content/stable-diffusion-pytorch\n",
        "!wget https://huggingface.co/jinseokim/stable-diffusion-pytorch-data/resolve/main/data.v20221029.tar\n",
        "!tar -xf data.v20221029.tar"
      ],
      "metadata": {
        "id": "NXnKKOxcMsin"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Run"
      ],
      "metadata": {
        "id": "UyM8vbLnWVNP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#@title Preload models (takes about ~20 seconds on default settings)\n",
        "\n",
        "from stable_diffusion_pytorch import model_loader\n",
        "models = model_loader.preload_models('cpu')"
      ],
      "metadata": {
        "cellView": "form",
        "id": "fGOopQsDS-7U"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@title Inference (takes about 30~40 seconds on default settings)\n",
        "\n",
        "from stable_diffusion_pytorch import pipeline\n",
        "\n",
        "prompt = \"a photograph of an astronaut riding a horse\"  #@param { type: \"string\" }\n",
        "prompts = [prompt]\n",
        "\n",
        "uncond_prompt = \"\"  #@param { type: \"string\" }\n",
        "uncond_prompts = [uncond_prompt] if uncond_prompt else None\n",
        "\n",
        "upload_input_image = False  #@param { type: \"boolean\" }\n",
        "input_images = None\n",
        "if upload_input_image:\n",
        "    from PIL import Image\n",
        "    from google.colab import files\n",
        "    print(\"Upload an input image:\")\n",
        "    path = list(files.upload().keys())[0]\n",
        "    input_images = [Image.open(path)]\n",
        "\n",
        "strength = 0.8  #@param { type:\"slider\", min: 0, max: 1, step: 0.01 }\n",
        "\n",
        "do_cfg = True  #@param { type: \"boolean\" }\n",
        "cfg_scale = 7.5  #@param { type:\"slider\", min: 1, max: 14, step: 0.5 }\n",
        "height = 512  #@param { type: \"integer\" }\n",
        "width = 512  #@param { type: \"integer\" }\n",
        "sampler = \"k_lms\"  #@param [\"k_lms\", \"k_euler\", \"k_euler_ancestral\"]\n",
        "n_inference_steps = 50  #@param { type: \"integer\" }\n",
        "\n",
        "use_seed = False  #@param { type: \"boolean\" }\n",
        "if use_seed:\n",
        "    seed = 42  #@param { type: \"integer\" }\n",
        "else:\n",
        "    seed = None\n",
        "\n",
        "pipeline.generate(prompts=prompts, uncond_prompts=uncond_prompts,\n",
        "                  input_images=input_images, strength=strength,\n",
        "                  do_cfg=do_cfg, cfg_scale=cfg_scale,\n",
        "                  height=height, width=width, sampler=sampler,\n",
        "                  n_inference_steps=n_inference_steps, seed=seed,\n",
        "                  models=models, device='cuda', idle_device='cpu')[0]"
      ],
      "metadata": {
        "cellView": "form",
        "id": "x_dhQfFYXoPu"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}
